
/**
 * 财务管理控制层
 */
define([ "app", "jquery", "md5", "ecb", "artDialog","iframeTools",
        "services/UserService",
        "services/TipsService",
		"resources/UserResource", 
		"resources/ShareResource"], function(app) {

	var deps = [ "$scope", "$state", "$modal","$location", "$stateParams","UserService","TipsService","UserResource","ShareResource" ];

	function controller($scope, $state, $modal,$location, $stateParams,UserService,TipsService,UserResource,ShareResource) {
		$scope.currentPage = 1;
        $scope.pages = [];
        $scope.pageSize = 10;
        //$scope.selected=10;
        queryData();
		
		function queryData(){
			
			 var content=$scope.content;
	    	 var nickName =$scope.nickName;
	    	 var shareType =$scope.shareType;
	    	 var shareStartDate =$scope.shareStartDate;
	         var shareEndDate =$scope.shareEndDate;
			
			ShareResource.queryShareList($scope.currentPage,$scope.pageSize,content,nickName,shareType,shareStartDate,shareEndDate).success(function(data){
				$scope.data = data.shareList;
				
				if(data.count % $scope.pageSize == 0){
         			$scope.totalPage=Math.floor(data.count/$scope.pageSize);
        		}else{
        			$scope.totalPage=Math.floor(data.count/$scope.pageSize)+1;
        		}
            });
		}
		
		$scope.$on('sharebroadcast', function() {
			$scope.keyWord="";
			queryData();
        })
        
		$scope.$on('refeshUser', function($event, data) {
			$scope.keyWord="";
			queryData();
        });
		$scope.gotoPage = function (currentNum) {
			
			if($scope.currentPage != currentNum){
				 $scope.currentPage = currentNum;
		            queryData();
			}
        };
        
        $scope.nextPage = function () {
        	if($scope.currentPage<$scope.totalPage){
        		$scope.currentPage++;
        		queryData();
        	}
        };

        $scope.upPage = function () {
        	if($scope.currentPage>1){
        		$scope.currentPage--;
        		queryData();
        	}	
        };
        
		
		$scope.addShare=function addShare($choseShare){
			$scope.data=null;
			queryData();
		};
		
		$scope.auditingShare=function auditingShare(shareId){
			
		};
		
		$scope.showShare=function showShare(shareId){
			$state.go('home.showShare', {
				shareId: shareId
            });
		};
		
		$scope.editShare=function editShare(shareId){
			$state.go('home.editShare', {
				shareId: shareId
            });
		};
		
		
		$scope.saveShare=function saveShare(shareId){
			
			var shareType=$("#shareType").val();
			if(undefined ==shareType || shareType==null || shareType ==""){
				art.dialog.alert("请选择分享类型");
			}
          	if(shareType=='1' || shareType =='2' || shareType=='4'){
          		var file =$('#textfield').val();
          		var formData = new FormData(document.forms.namedItem("shareForm")); 
          		
          		var file1 =$('#textfield1').val();
    			if(shareType=='1'){
    				if(file1 ==null || file1==""){
        				art.dialog.alert("请选择视频封面");
        				return;
        			}
    			}
    			
    			if(file ==null || file==""){
    				art.dialog.alert("请选择上传文件!");
    				return;
    			}
          	}
          	
          	if(shareType=='5'){
          		var linkUrl =$scope.shareVo.linkUrl;
              	
              	if(linkUrl==null || linkUrl ==''){
              		art.dialog.alert("链接地址不能为空!");
    				return;
              	}
              	
              	if(!linkUrl.startWith("http://") && !linkUrl.startWith("https://")){
              		art.dialog.alert("链接地址无效!");
    				return;
              	}
          	}
          	
          	var comData =null;
          	if(shareType=='5'){
          		comData ={shareType:$scope.shareVo.shareType,content:$scope.shareVo.content,linkUrl:$scope.shareVo.linkUrl};
          	}else{
          		comData ={shareType:$scope.shareVo.shareType,content:$scope.shareVo.content};
          	}
			ShareResource.saveFormShare(comData,formData).success(function(data){
				var json = eval('(' + data.fileId + ')');
				if(json.obj== undefined || json.obj==null || json.obj =="" || json.resCode!='0000' ){
					art.dialog.alert("分享失败!")
					return;
				}
				
				if(shareType=='1' || shareType =='2' || shareType=='4'){
					if(json.obj.shareUrl.lenght<0){
						TipsService.show("分享失败!");
						return;
					}else{
						TipsService.show("分享成功!");
						$state.go('home.shareManagement');
					}
				}else{
					TipsService.show("分享成功!");
					$state.go('home.shareManagement');
				}
	        });
		};
		
		$scope.addShare=function addShare(){
			$state.go('home.addShare');
		};
		$scope.cancel=function cancel(){
			$state.go('home.shareManagement');
		};
		
		$scope.changeType =function(){
			var shareType=$("#shareType").val();
          	if(shareType=='1'){
          		$("#videoPic").show();
          		$("#fileAttachs").show();
          		$("#linkUrl").hide();
          		
          	}else if(shareType =='2' || shareType=='4'){
          		$("#fileAttachs").show();
          		$("#linkUrl").hide();
          		$("#videoPic").hide();
          	}
          	else {
          		$("#fileAttachs").hide();
          	}
          	if(shareType=='5'){
          		$("#linkUrl").show();
          	}
		};
		
		$scope.changePageSize =function(){
	          	$scope.currentPage =1;
	          	ShareResource.queryShareList($scope.currentPage,$scope.pageSize).success(function(data){
					$scope.data = data.shareList;
					if(data.count % $scope.pageSize == 0){
	         			$scope.totalPage=Math.floor(data.count/$scope.pageSize);
	        		}else{
	        			$scope.totalPage=Math.floor(data.count/$scope.pageSize)+1;
	        		}
	            });
	     };
	     
	     
	     $scope.updateStatus =function (shareId ,status){
	    	 ShareResource.updateStatus(shareId ,status).success(function(data){
	    		 queryData();
	         });
	    	 
	     };
	     
	     
	     $scope.search=function (){
	    	 var content=$scope.content;
	    	 var nickName =$scope.nickName;
	    	 var shareType =$scope.shareType;
	    	 var shareStartDate =$scope.shareStartDate;
	         var shareEndDate =$scope.shareEndDate;
	    	 ShareResource.queryShareList(0,$scope.pageSize,content,nickName,shareType,shareStartDate,shareEndDate).success(function(data){
					$scope.data = data.shareList;
					$scope.currentPage =1;
					if(data.count % $scope.pageSize == 0){
	         			$scope.totalPage=Math.floor(data.count/$scope.pageSize);
	        		}else{
	        			$scope.totalPage=Math.floor(data.count/$scope.pageSize)+1;
	        		}
            });
	    	 
	     };
	     
	     $scope.deleteShare =function (shareId){
	    	 ShareResource.deleteShare(shareId).success(function(data){
	    		 queryData();
	         });
	     };
		
	}
	
	String.prototype.startWith=function(s){
	  if(s==null||s==""||this.length==0||s.length>this.length)
	   return false;
	  if(this.substr(0,s.length)==s)
	     return true;
	  else
	     return false;
	  return true;
	 }
	
	 
	controller.$inject = deps;
	return app.lazy.controller("ShareManagementController", controller);
});
